<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:goods:add']"
        >新增
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="goodsList" >
<!--      <el-table-column type="selection" width="55" align="center"/>      @selection-change="handleSelectionChange"        -->
      <!--      <el-table-column label="商品详细介绍，是富文本格式" align="center" prop="goodsId" />-->
      <el-table-column label="部门名称" align="center" prop="deptName"/>
      <el-table-column label="商品名称" align="center" prop="name"/>
      <el-table-column label="商品所属类目" align="center" prop="categoryName"/>
      <el-table-column label="商品类型" align="center" prop="type" :formatter="typeFormat"/>
      <el-table-column label="商品简介" align="center" prop="brief"/>

      <el-table-column label="是否上架" align="center"   >
        <template slot-scope="scope" >
          <el-switch
            @change = "switchChange($event,scope.row)"
            v-model="scope.row.isOnSaleBool">
          </el-switch>
        </template>
      </el-table-column>

      <!--      <el-table-column label="是否上架" align="center" prop="sortOrder" />-->
      <el-table-column label="商品单位" align="center" prop="unit"/>
      <el-table-column label="专柜价格" align="center" prop="counterPrice"/>
      <el-table-column label="零售价格" align="center" prop="retailPrice"/>
      <!--      <el-table-column label="商品详细介绍，是富文本格式" align="center" prop="detail" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="normal"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:goods:edit']"
          >修改
          </el-button>
          <el-button
            size="normal"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:goods:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="部门名" prop="deptId">
          <el-input v-model="form.deptName" placeholder="请输入部门ID"/>
        </el-form-item>
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名称"/>
        </el-form-item>
        <el-form-item label="商品所属类目ID" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入商品所属类目ID"/>
        </el-form-item>
        <el-form-item label="商品简介" prop="brief">
          <el-input v-model="form.brief" placeholder="请输入商品简介"/>
        </el-form-item>
        <el-form-item label="是否上架" prop="isOnSale">
          <el-input v-model="form.isOnSale" placeholder="请输入是否上架"/>
        </el-form-item>
        <el-form-item label="是否上架" prop="sortOrder">
          <el-input v-model="form.sortOrder" placeholder="请输入是否上架"/>
        </el-form-item>
        <el-form-item label="商品单位，例如件、盒" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入商品单位，例如件、盒"/>
        </el-form-item>
        <el-form-item label="专柜价格" prop="counterPrice">
          <el-input v-model="form.counterPrice" placeholder="请输入专柜价格"/>
        </el-form-item>
        <el-form-item label="零售价格" prop="retailPrice">
          <el-input v-model="form.retailPrice" placeholder="请输入零售价格"/>
        </el-form-item>
        <el-form-item label="商品详细介绍，是富文本格式" prop="detail">
          <el-input v-model="form.detail" placeholder="请输入商品详细介绍，是富文本格式"/>
        </el-form-item>
        <el-form-item label="商品详细介绍，是富文本格式" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入商品详细介绍，是富文本格式"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  let that = this
  import { listGoods, getGoods, delGoods, addGoods, updateGoods, exportGoods ,editGoods } from '@/api/goods'

  export default {
    name: 'Goods',
    data() {
      return {
        typeOptions: [],//1 电子水票 2 纸质水票  3水
        waterStatusOptions: [],//0上架  1下架
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 【请填写功能名称】表格数据
        goodsList: [],
        isOnSaleBool:null,  //isOnSale 0上架  1下架     isOnSaleBool  true上架  false下架
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          deptId: undefined,
          name: undefined,
          categoryId: undefined,
          brief: undefined,
          isOnSale: undefined,
          sortOrder: undefined,
          unit: undefined,
          counterPrice: undefined,
          retailPrice: undefined,
          detail: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    created() {
      this.getList()
      //1 电子水票 2 纸质水票  3水
      this.getDicts('sys_water_type').then(response => {
        this.typeOptions = response.data
      })
      //  sys_water_status   0上架  1下架
      this.getDicts('sys_water_status').then(response => {
        this.waterStatusOptions = response.data
      })
    },
    methods: {
      /** 删除按钮操作 */
      handleDelete(row) {
        const goodsId = row.goodsId

          this.$confirm('是否确认删除吗?', '警告', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.deloneGoods(goodsId)
          }).catch(function() {
          })




      },
      //删除方法
      deloneGoods(goodsId){
        delGoods(goodsId).then(res=>{
          console.log(res)
          if (res.code===200){
            this.msgSuccess('操作成功')
            this.getList()
          }
        })
      },
      //是否上架操作
      switchChange(e,row){
        var goodsId = row.goodsId       //isOnSale 0上架  1下架     isOnSaleBool  true上架  false下架
        var obj
        if (e==true){
          obj={
            goodsId:goodsId,
            isOnSale:0
          }
        }else if(e==false){
          obj={
            goodsId:goodsId,
            isOnSale:1
          }
        }

        editGoods(obj).then(res=>{
          console.log(res)
          if (res.code===200){
            this.msgSuccess('操作成功')
          }
          this.getList()
        })

      },
      //商品状态翻译
      typeFormat(row, column) {
        return this.selectDictLabel(this.typeOptions, row.type)
      },
      //是否上架翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.waterStatusOptions, row.isOnSale)
      },
      /** 查询【请填写功能名称】列表 */
      getList() {
        this.loading = true
        listGoods(this.queryParams).then(response => {
          console.log(response)
          if (response.rows.length>0){
            response.rows.forEach((items,i)=>{
              items.isOnSaleBool = items.isOnSale==0?true:false
             //  //isOnSale 0上架  1下架     isOnSaleBool  true上架  false下架
            })
          }
          this.goodsList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          goodsId: undefined,
          deptId: undefined,
          name: undefined,
          categoryId: undefined,
          brief: undefined,
          isOnSale: undefined,
          sortOrder: undefined,
          unit: undefined,
          counterPrice: undefined,
          retailPrice: undefined,
          detail: undefined,
          delFlag: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.goodsId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        // this.open = true;
        // this.title = "添加【请填写功能名称】";
        this.$router.push(`/goods/create`)
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        this.$router.push(`/GoodsList/edit/${row.goodsId}`)
        // const goodsId = row.goodsId || this.ids
        // getGoods(goodsId).then(response => {
        //   this.form = response.data;
        //   this.open = true;
        //   this.title = "修改【请填写功能名称】";
        // });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.goodsId != undefined) {
              updateGoods(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.getList()
                } else {
                  this.msgError(response.msg)
                }
              })
            } else {
              addGoods(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('新增成功')
                  this.open = false
                  this.getList()
                } else {
                  this.msgError(response.msg)
                }
              })
            }
          }
        })
      },
      // /** 删除按钮操作 */
      // handleDelete(row) {
      //   const goodsIds = row.goodsId || this.ids
      //   this.$confirm('是否确认删除【请填写功能名称】编号为"' + goodsIds + '"的数据项?', '警告', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(function() {
      //     return delGoods(goodsIds)
      //   }).then(() => {
      //     this.getList()
      //     this.msgSuccess('删除成功')
      //   }).catch(function() {
      //   })
      // },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有【请填写功能名称】数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return exportGoods(queryParams)
        }).then(response => {
          this.download(response.msg)
        }).catch(function() {
        })
      }
    }
  }
</script>
